package com.ihd.ihardware.view.tzc.health.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityWalkBinding;
import com.ihd.ihardware.db.StepDataDao;
import com.ihd.ihardware.pojo.StepEntity;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.health.viewmodel.WalkViewModel;
import com.ihd.ihardware.view.tzc.me.view.TargetStepActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class WalkActivity extends BaseActivity<ActivityWalkBinding, WalkViewModel> {
    private float mSteps = 0.0f;
    private float weight = 0.0f;
    private BroadcastReceiver receiver;
    private UserInfoRes.DataBean ud;
    private MDialog mAlert;
    private List<StepEntity> stepEntityList = new ArrayList<>();
    private StepDataDao stepDataDao;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.hisIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(WalkActivity.this, WalkHisActivity.class);
                i.putExtra("weight", weight);
                startActivity(i);
            }
        });

        weight = Float.parseFloat(TextUtils.isEmpty(getIntent().getStringExtra("weight")) ? "0.0" : getIntent().getStringExtra("weight"));
    }

    @Override
    protected void onResume() {
        super.onResume();
        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (AppConstans.SEND_STEP.equals(action)) {
                    mSteps = intent.getIntExtra("step", 0);
                    binding.walkTV.setText(String.valueOf((int) mSteps));
                    if (!TextUtils.isEmpty(ud.getDreamStepNumber()) && !"0".equals(ud.getDreamStepNumber())) {
                        binding.circleView.setProgressNum(mSteps, 0);
                    }
                    binding.distance.setText(new BigDecimal(mSteps * 0.0006).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    binding.calulee.setText(new BigDecimal(weight * mSteps * 0.0006 * 1.036).setScale(1, BigDecimal.ROUND_HALF_UP).toString() + "k");
                }
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(AppConstans.SEND_STEP);
        registerReceiver(receiver, filter);

        stepDataDao = new StepDataDao(this);
        stepEntityList.clear();
        stepEntityList.addAll(stepDataDao.getAllDatas());

        int size = stepEntityList.size() > 30 ? 30 : stepEntityList.size();
        int totalStep = 0;
        for (int i = 0; i < size; i++) {
            totalStep += Integer.parseInt(stepEntityList.get(i).getSteps());
        }
        binding.saveAll.setText(new BigDecimal(totalStep * 0.0006 * 0.07).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "升");
        binding.perStep.setText(new BigDecimal(totalStep / size).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
        binding.perDistance.setText(new BigDecimal(totalStep / size * 0.0006).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        binding.reduceAll.setText(new BigDecimal(weight * totalStep * 0.0006 * 1.036 / 7.7).setScale(0, BigDecimal.ROUND_HALF_UP).toString() + "克");

        if (!TextUtils.isEmpty(ud.getDreamStepNumber()) && !"0".equals(ud.getDreamStepNumber())) {
            binding.circleView.setMaxNum(Integer.parseInt(ud.getDreamStepNumber()));
            binding.targetStep.setText("目标 " + ud.getDreamStepNumber() + "步");
        } else {
            mAlert = DialogUtils.alertDialog(WalkActivity.this, MDialog.DG_TYPE.ALERT, "请先设置目标步数", "取消", "确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlert.cancel();
                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlert.cancel();
                    Intent i = new Intent(WalkActivity.this, TargetStepActivity.class);
                    startActivity(i);
                }
            });
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_walk;
    }

    @Override
    protected Class getViewModelClass() {
        return WalkViewModel.class;
    }
}
